package com.mozai.shopping.service.impl;


import com.mozai.shopping.mapper.ProductTypeMapper;
import com.mozai.shopping.pojo.PageResp;
import com.mozai.shopping.pojo.ProductType;
import com.mozai.shopping.service.ProductTypeService;
import com.mozai.shopping.utils.bean.CommonQueryBean;
import com.mysql.cj.log.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author wj183
* @description 针对表【product_type】的数据库操作Service实现
* @createDate 2024-09-29 08:23:01
*/
@Service
@Slf4j
public class ProductTypeServiceImpl implements ProductTypeService {

    @Autowired
    private ProductTypeMapper productTypeMapper;

    /**
     * 商品类型新增
     * @param productType
     */
    @Override
    public void insertProductType(ProductType productType) {
           productTypeMapper.insert(productType);
    }


    /**
     * 商品类型删除
     * @param productType
     */
    @Override
    public void delProductType(ProductType productType) {
        productType.setDelFlag(1);
        productTypeMapper.updateByPrimaryKeySelective(productType);
    }


    /**
     * 商品类型修改
     * @param productType
     */
    @Override
    public void modifyProductType(ProductType productType) {
        if (productTypeMapper.selectByPrimaryKey(productType.getId()).getDelFlag() == 0) {
            productType.setDelFlag(0);
            productTypeMapper.updateByPrimaryKeySelective(productType);

        }else {
            log.error("商品已经被删除无法修改！");
        }
    }


    /**
     * 商品类型分页查询
     * @param productType
     * @param commonQueryBean
     * @return
     */
    @Override
    public PageResp<ProductType> queryProductTypeList(ProductType productType, CommonQueryBean commonQueryBean) {
        if(commonQueryBean!=null && commonQueryBean.getPageNum()!=null && commonQueryBean.getPageSize()!=null){
            commonQueryBean.setStart((commonQueryBean.getPageNum()-1)*commonQueryBean.getPageSize());
        }
        List<ProductType> productTypeResult = productTypeMapper.list4Page(productType,commonQueryBean);
        //查询总条数
        int count = productTypeMapper.count(productType);
        commonQueryBean.setTotal(count);
        commonQueryBean.setTotalPage(CommonQueryBean.calculateTotalPage(commonQueryBean));
        PageResp<ProductType> productTypePageResp = new PageResp<>();
        productTypePageResp.setList(productTypeResult);
        productTypePageResp.setPageNum(commonQueryBean.getPageNum());
        productTypePageResp.setPageSize(commonQueryBean.getPageSize());
        productTypePageResp.setTotalPage(commonQueryBean.getTotalPage());
        productTypePageResp.setStart(commonQueryBean.getStart());
        productTypePageResp.setTotal(commonQueryBean.getTotal());
        return productTypePageResp;

    }
}




